package com.bonc.hbase.api.ctrl;


import java.io.IOException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bonc.hbase.api.client.HbaseConvertUtil;
import com.bonc.hbase.api.client.HbaseOperateClient;
import com.bonc.hbase.api.constant.BaseConstant;
import com.bonc.hbase.api.entity.JsonResult;
import com.bonc.hbase.api.util.ValidateRequestUtil;


/**
 * 查询位置接口类
 * 
 * @author caiqiang
 * @version 2016年8月3日
 * @see PositionApiController
 * @since
 */
@RestController
@RequestMapping("/api/v1")
public class PositionApiController {
    /**
     * HBase 数据操作客户端
     */
    @Autowired
    private HbaseOperateClient hbaseOperateClient;

    /**
     * 日志对象
     */
    private static final Logger LOG = LoggerFactory.getLogger(PositionApiController.class);

    /**
     * Description: 查询位置
     * 
     * @param phone
     * @param request
     * @return Object
     * @see
     */
    @RequestMapping(value = {"/position"}, method = RequestMethod.GET)
    public Object getPosition(String phone, HttpServletRequest request) {

        if (!ValidateRequestUtil.validate(request)) {
            return new JsonResult(BaseConstant.CODE_TOKEN_ILLEGAL, BaseConstant.MSG_TOKEN_ILLEGAL);
        }

        if (StringUtils.isEmpty(phone)) {
            return new JsonResult(BaseConstant.CODE_PARAM_ERROR, BaseConstant.MSG_PARAM_ERROR);
        }

        byte[] rowkey = convertToPhoneByte(phone);
        Result result;
        try {
            result = hbaseOperateClient.getResult(rowkey, "current_position");
        }
        catch (IOException e) {
            LOG.error(e.getMessage());
            return new JsonResult(BaseConstant.CODE_ERROR, BaseConstant.MSG_ERROR);
        }

        HashMap<String, Integer> columnMap = new HashMap<String, Integer>();
        columnMap.put("longitude", 0);
        columnMap.put("latitude", 1);
        columnMap.put("createTime", 2);
        columnMap.put("provinceId", 3);
        columnMap.put("cityId", 4);
        JSONObject returnObj = HbaseConvertUtil.result2JSONObj(columnMap, result, "f", "q");
        if (returnObj == null) {
            return new JsonResult(BaseConstant.CODE_DATA_NONEXIST, BaseConstant.MSG_DATA_NONEXIST);
        }
        return new JsonResult(returnObj);
    }

    /**
     * Description:创建rowkey
     * 
     * @param phone
     * @return byte[]
     * @see
     */
    private byte[] convertToPhoneByte(String phone) {
        byte[] phoneId = Bytes.copy(Bytes.toBytes(Long.parseLong(phone)), 3, 5);
        byte[] deviceNumberHash = Bytes.toBytes((short)(phone.hashCode() & 0x7fff));
        byte[] rowkey = Bytes.add(deviceNumberHash, phoneId);

        return rowkey;

    }
}
